#include "BlueprintScriptLibrary/Reflections.h"
#include "BlueprintScriptLibrary/Pin.h"
namespace BlueprintScriptLibrary
{
    void SolvePin(Pin* pin)
    {
       // Pin* next = *pin->GetConnectPin();
        Pin* next = pin;
        while (next != nullptr)
        {
            if (BranchPin* branch = dynamic_cast<BranchPin*>(next))
            {
                if (branch->GetCheckVal().val)
                {
                    next = branch->nextCheckedPin;
                }
                else {
                    next = branch->nextUnCheckedPin;
                }
            }
            else if (SequencePin* sequence = dynamic_cast<SequencePin*>(next))
            {
                Pin* pin = *sequence->GetConnectPin();
                int nums = sequence->GetOutputPinNums();
                for (int i = 0; i < nums; i++)
                {
                    SolvePin(pin);
                }
            }
            else if (MethodPin* method = dynamic_cast<MethodPin*>(next))
            {
                method->Execute(method->obj);
                Pin* pin = *method->GetConnectPin();
                next = pin;
            }
            else if (FunctionPin* func = dynamic_cast<FunctionPin*>(next))
            {
                func->Execute();
                //Pin* pin = fun//*func->GetConnectPin();
                //next = pin;
                next = func->next;
            }
            else if (ForLoopPin* flp = dynamic_cast<ForLoopPin*>(next))
            {
                for (int i = flp->GetStartIndex(); i < flp->GetCompleteIndex();i++)
                {
                    SolvePin(flp->loopBody);
                }
                SolvePin(flp->compeleteBody);
            }
        }
    }
    //template<typename T>
    // T* NewObj()
    //{
    //    T* t = new T();
    //    /* BlueprintScriptLibrary::Object* d = dynamic_cast<BlueprintScriptLibrary::Object*>(t);
    //     if (d) {*/
    //    static GC<T> gc;
    //    gc.objs.push_back(t);
    //    return t;
    //    /* }
    //     delete t;
    //     printf("Can't NewObj a object,it's type must a child of Object!\r\n");
    //     return nullptr;*/
    //}
}